<?php

class CurrencyCreateWidget extends CWidget {

    public $visible = true;

    public function init() {
        
    }

    public function run() {
        if ($this->visible) {
            $this->renderContent();
        }
    }

    protected function renderContent() {
        $model = new Currency;
        settings()->deleteCache();
        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'updateForm') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if (isset($_POST['Currency'])) {
            $currency = new Currency;
            $currency->codeTo = $_POST['Currency']['codeTo'];
            $from = 'USD';
            $to = $_POST['Currency']['codeTo'];
            $cc = new ECurrencyHelper();
            $rate1 = $cc->convert($from, $to, 1, ECurrencyHelper::USE_YAHOO);
            $currency->codeFrom = $from;
            $currency->codeTo = $to;
            $currency->conversionRate = $rate1;
            $currency->updatedTime = time();
            if ($currency->save()) {
                user()->setFlash('success', t('cms', 'Updated Successfully!'));
            }
        }
        $this->render('cmswidgets.views.shopping.currency_create_widget', array('model' => $model));
    }

}
